package com.wechat.back.service.impl;

import com.wechat.back.mapper.CategoryMapper;
import com.wechat.back.pojo.articles.Category;
import com.wechat.back.service.CategoryService;
import com.wechat.back.service.SensitiveWordService;
import com.wechat.back.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static com.wechat.back.utils.SensitiveWordRead.readSensitiveWords;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Override
    public void add(Category category) {
        List<String> SensitiveWords = readSensitiveWords();
        sensitiveWordService.loadSensitiveWord(SensitiveWords); //加载敏感词
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("adminId");
        category.setCreateUser(userId);
        String categoryName = category.getCategoryName();
        if (sensitiveWordService.hasSensitiveWord(categoryName)){
            //包含敏感词
            category.setCategoryState("待审核");
        }else {
            //不包含敏感词
            category.setCategoryState("已通过");
        }
        categoryMapper.add(category);
    }

    @Override
    public List<Category> list() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return categoryMapper.list(userId);
    }

    @Override
    public Category findById(Integer id) {
        Category c = categoryMapper.findById(id);
        return c;
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> allList() {
        return categoryMapper.allList();
    }

    //查询通过的分类
    @Override
    public List<Category> allowList() {
        String categoryState = "已通过";
        return categoryMapper.findState(categoryState);
    }
    //查询驳回的分类
    @Override
    public List<Category> disallowList() {
        String categoryState = "已驳回";
        return categoryMapper.findState(categoryState);
    }
    //查询待审核的分类
    @Override
    public List<Category> inspectList() {
        String categoryState = "待审核";
        return categoryMapper.findState(categoryState);
    }
}
